﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ATSBLL;
using ATSModel;

public partial class Airtightness_AirtightnessConfigList : System.Web.UI.Page
{

    private AirtightnessConfig ac;
    

    /// <summary>
    /// 无参数构造
    /// </summary>
    public Airtightness_AirtightnessConfigList()
    {
        ac = new AirtightnessConfig();
    }

     /// <summary>
     /// 页面加载
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            Bind();
        }
    }

   /// <summary>
   /// 根据产品类型检索气密配置
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
    string PtSn=string.Empty;
    protected void butQuery_Click(object sender, EventArgs e)
    {
        PtSn = this.tbProductTypeSn.Text.Trim();
        Bind();
    }

    /// <summary>
    /// 查询所有气密配置列表数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void butAllPtList_Click(object sender, EventArgs e)
    {
        PtSn = string.Empty;
        Bind();
    } 

    #region  GridView  插入  编辑更新 删除 翻页 行绑定事件
    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView.PageIndex = e.NewPageIndex;
        Bind();
    }

    /// <summary>
    /// 编辑取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView.EditIndex = -1;
        Bind();
    }

    /// <summary>
    /// 行绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[11].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    /// <summary>
    /// 行删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(GridView.DataKeys[e.RowIndex].Values[2].ToString());
        ac.Delete(id);
        Bind();
    }

    /// <summary>
    /// 行编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView.EditIndex = e.NewEditIndex;
        Bind();
    }

    /// <summary>
    /// 行更新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        AirtightnessConfigInfo airtightnessConfigInfo = new AirtightnessConfigInfo();
        airtightnessConfigInfo.ID = int.Parse(GridView.DataKeys[e.RowIndex].Values[2].ToString());
        airtightnessConfigInfo.ProductTypeID = "";
        airtightnessConfigInfo.ChannelNo = 0;
        try
        {
            airtightnessConfigInfo.SpecValue = int.Parse(((TextBox)(GridView.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim());
            airtightnessConfigInfo.InflateDuration = int.Parse(((TextBox)(GridView.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim());
            airtightnessConfigInfo.BalanceDuration1 = int.Parse(((TextBox)(GridView.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim());
            airtightnessConfigInfo.BalanceDuration2 = int.Parse(((TextBox)(GridView.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim());
            airtightnessConfigInfo.MeasureDuration = int.Parse(((TextBox)(GridView.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim());
            airtightnessConfigInfo.LeakSpec = int.Parse(((TextBox)(GridView.Rows[e.RowIndex].Cells[7].Controls[0])).Text.Trim());
            airtightnessConfigInfo.MinusLeakSpec = int.Parse(((TextBox)(GridView.Rows[e.RowIndex].Cells[8].Controls[0])).Text.Trim());
            airtightnessConfigInfo.ProductCubage = int.Parse(((TextBox)(GridView.Rows[e.RowIndex].Cells[9].Controls[0])).Text.Trim());
        }
        catch(FormatException)
        {
            string str1 = "规格值输入不正确，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }

        ac.Update(airtightnessConfigInfo);
        GridView.EditIndex = -1;
        Bind();
    }

    protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView.DataSource = null;
            GridView.DataBind();
        }
    }

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DetailsView_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView.Controls[0].Controls[0].FindControl("DetailsView");
        AirtightnessConfigInfo airtightnessConfigInfo = new AirtightnessConfigInfo();
        airtightnessConfigInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        try
        {
            airtightnessConfigInfo.ChannelNo = int.Parse(((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim());
            airtightnessConfigInfo.SpecValue = int.Parse(((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim());
            airtightnessConfigInfo.InflateDuration = int.Parse(((TextBox)(d.Rows[3].Cells[1].Controls[0])).Text.Trim());
            airtightnessConfigInfo.BalanceDuration1 = int.Parse(((TextBox)(d.Rows[4].Cells[1].Controls[0])).Text.Trim());
            airtightnessConfigInfo.BalanceDuration2 = int.Parse(((TextBox)(d.Rows[5].Cells[1].Controls[0])).Text.Trim());
            airtightnessConfigInfo.MeasureDuration = int.Parse(((TextBox)(d.Rows[6].Cells[1].Controls[0])).Text.Trim());
            airtightnessConfigInfo.LeakSpec = int.Parse(((TextBox)(d.Rows[7].Cells[1].Controls[0])).Text.Trim());
            airtightnessConfigInfo.MinusLeakSpec = int.Parse(((TextBox)(d.Rows[8].Cells[1].Controls[0])).Text.Trim());
            airtightnessConfigInfo.ProductCubage = int.Parse(((TextBox)(d.Rows[9].Cells[1].Controls[0])).Text.Trim());
        }
        catch(FormatException) 
        {
            string str1 = "规格值输入不正确，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            return;
        }

        TProductType p = new TProductType();
        TProductTypeInfo pi = p.LoadEntity(airtightnessConfigInfo.ProductTypeID);
        if (string.IsNullOrEmpty(pi.ProductTypeID))
        {
            string str1 = "此产品类型不存在，必须是在产品类型信息中录入过的产品！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            AirtightnessConfigInfo aro = ac.Airtightness_GetbyPIDCHNO(airtightnessConfigInfo.ProductTypeID, airtightnessConfigInfo.ChannelNo);
            if (!string.IsNullOrEmpty(aro.ProductTypeID))
            {
                string str1 = "此产品类型在此通道的气密信息已存在！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            }
            else
            {
                ac.Insert(airtightnessConfigInfo);
                string str1 = "添加成功！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                Bind();
            }
        }
    }
 
    protected void DetailsView_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        Bind();
    }

  
    #endregion

    /// <summary>
    /// 绑定页面数据
    /// </summary>
    protected void Bind()
    {
        if (string.IsNullOrEmpty(PtSn))
            this.GridView.DataSource = ac.LoadEntities();
        else
            GridView.DataSource = ac.Airtightness_GetbyPID(PtSn);
            GridView.DataBind();
    }


}
